<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单词" prop="word">
        <el-input
          v-model="queryParams.word"
          placeholder="请输入单词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="${comment}" prop="projectId">-->
<!--        <el-input-->
<!--          v-model="queryParams.projectId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="isDelete">-->
<!--        <el-input-->
<!--          v-model="queryParams.isDelete"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="音标" prop="phonetics">-->
<!--        <el-input-->
<!--          v-model="queryParams.phonetics"-->
<!--          placeholder="请输入音标"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="voice">-->
<!--        <el-input-->
<!--          v-model="queryParams.voice"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="groupNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.groupNo"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="source">-->
<!--        <el-input-->
<!--          v-model="queryParams.source"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="vocabularyBookId">-->
<!--        <el-input-->
<!--          v-model="queryParams.vocabularyBookId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="sentenceDictionary">-->
<!--        <el-input-->
<!--          v-model="queryParams.sentenceDictionary"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="wordClassDictionary">-->
<!--        <el-input-->
<!--          v-model="queryParams.wordClassDictionary"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="synonymDictionary">-->
<!--        <el-input-->
<!--          v-model="queryParams.synonymDictionary"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="meaningDictionary">-->
<!--        <el-input-->
<!--          v-model="queryParams.meaningDictionary"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="translationDictionary">-->
<!--        <el-input-->
<!--          v-model="queryParams.translationDictionary"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="paradigmId">-->
<!--        <el-input-->
<!--          v-model="queryParams.paradigmId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="单词类别" prop="wordCategory">-->
<!--        <el-input-->
<!--          v-model="queryParams.wordCategory"-->
<!--          placeholder="请输入单词类别"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="单词类别test(含义待明确)" prop="wordCategoryTest">-->
<!--        <el-input-->
<!--          v-model="queryParams.wordCategoryTest"-->
<!--          placeholder="请输入单词类别test(含义待明确)"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="造句" prop="sentence">-->
<!--        <el-input-->
<!--          v-model="queryParams.sentence"-->
<!--          placeholder="请输入造句"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="故事组编号" prop="storyGroupNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.storyGroupNo"-->
<!--          placeholder="请输入故事组编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:vocabulary:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:vocabulary:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:vocabulary:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:vocabulary:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vocabularyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="单词" align="center" prop="word" />
<!--      <el-table-column label="${comment}" align="center" prop="projectId" />-->
<!--      <el-table-column label="${comment}" align="center" prop="isDelete" />-->
      <el-table-column label="音标" align="center" prop="phonetics" />
      <el-table-column label="发声" align="center" prop="voice">
        <template slot-scope="scope">
          <el-button type="text" @click="playAudio(scope.row.voice)" icon="el-icon-headset"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="${comment}" align="center" prop="groupNo" />-->
<!--      <el-table-column label="${comment}" align="center" prop="source" />-->
      <el-table-column label="词库ID" align="center" prop="vocabularyBookId" />
      <el-table-column label="状态" align="center" prop="status" />
<!--      <el-table-column label="${comment}" align="center" prop="sentenceDictionary" />-->
<!--      <el-table-column label="${comment}" align="center" prop="wordClassDictionary" />-->
<!--      <el-table-column label="${comment}" align="center" prop="synonymDictionary" />-->
<!--      <el-table-column label="${comment}" align="center" prop="meaningDictionary" />-->
<!--      <el-table-column label="${comment}" align="center" prop="translationDictionary" />-->
<!--      <el-table-column label="${comment}" align="center" prop="paradigmId" />-->
      <el-table-column label="单词类别" align="center" prop="wordCategory" />
<!--      <el-table-column label="单词类别test(含义待明确)" align="center" prop="wordCategoryTest" />-->
      <el-table-column label="造句" align="center" prop="sentence" />
      <el-table-column label="故事组编号" align="center" prop="storyGroupNo" />
      <el-table-column label="故事组例句" align="center" prop="storyGroupExampleSentence" />
      <el-table-column label="故事梗概" align="center" prop="storyMain" />
      <el-table-column label="更多" align="center" >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleDetail(scope.row.id)"
          >更多内容</el-button>
        </template>

      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:vocabulary:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:vocabulary:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改正式词库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单词" prop="word">
          <el-input v-model="form.word" placeholder="请输入单词" />
        </el-form-item>
        <el-form-item label="项目ID" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目ID" />
        </el-form-item>
<!--        <el-form-item label="${comment}" prop="isDelete">-->
<!--          <el-input v-model="form.isDelete" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
        <el-form-item label="音标" prop="phonetics">
          <el-input v-model="form.phonetics" placeholder="请输入音标" />
        </el-form-item>
        <el-form-item label="发音" prop="voice">
          <el-input v-model="form.voice" placeholder="请输入发音url" />
        </el-form-item>
        <el-form-item label="分组编号" prop="groupNo">
          <el-input v-model="form.groupNo" placeholder="请输入分组编号" />
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="词库ID" prop="vocabularyBookId">
          <el-input v-model="form.vocabularyBookId" placeholder="请输入词库ID" />
        </el-form-item>
<!--        <el-form-item label="${comment}" prop="sentenceDictionary">-->
<!--          <el-input v-model="form.sentenceDictionary" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="wordClassDictionary">-->
<!--          <el-input v-model="form.wordClassDictionary" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="synonymDictionary">-->
<!--          <el-input v-model="form.synonymDictionary" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="meaningDictionary">-->
<!--          <el-input v-model="form.meaningDictionary" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="translationDictionary">-->
<!--          <el-input v-model="form.translationDictionary" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="paradigmId">-->
<!--          <el-input v-model="form.paradigmId" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
        <el-form-item label="单词类别" prop="wordCategory">
          <el-input v-model="form.wordCategory" placeholder="请输入单词类别" />
        </el-form-item>
<!--        <el-form-item label="单词类别test(含义待明确)" prop="wordCategoryTest">-->
<!--          <el-input v-model="form.wordCategoryTest" placeholder="请输入单词类别test(含义待明确)" />-->
<!--        </el-form-item>-->
        <el-form-item label="造句" prop="sentence">
          <el-input v-model="form.sentence" placeholder="请输入造句" />
        </el-form-item>
        <el-form-item label="故事组编号" prop="storyGroupNo">
          <el-input v-model="form.storyGroupNo" placeholder="请输入故事组编号" />
        </el-form-item>
        <el-form-item label="故事组例句" prop="storyGroupExampleSentence">
          <el-input v-model="form.storyGroupExampleSentence" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="故事梗概" prop="storyMain">
          <el-input v-model="form.storyMain" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVocabulary, getVocabulary, delVocabulary, addVocabulary, updateVocabulary } from "@/api/wordmemory/vocabulary";

export default {
  name: "Vocabulary",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 正式词库表格数据
      vocabularyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        word: null,
        projectId: null,
        isDelete: null,
        phonetics: null,
        voice: null,
        groupNo: null,
        source: null,
        vocabularyBookId: null,
        status: null,
        sentenceDictionary: null,
        wordClassDictionary: null,
        synonymDictionary: null,
        meaningDictionary: null,
        translationDictionary: null,
        paradigmId: null,
        wordCategory: null,
        wordCategoryTest: null,
        sentence: null,
        storyGroupNo: null,
        storyGroupExampleSentence: null,
        storyMain: null
      },
      audio: new Audio(), // 创建一个audio实例

      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const bankId = this.$route.params && this.$route.params.id;
    console.log(`vocabularyBookId is ${bankId}`)
    this.queryParams.vocabularyBookId = bankId;
    this.getList();
  },
  methods: {
    playAudio(url) {
      this.audio.src = url; // 设置音频的src属性为点击的音频URL
      this.audio.play(); // 播放音频
    },
    handleDetail(id) {
      this.$router.push('/mmdou/vocabulary-info/index/' + id);
    },
    /** 查询正式词库列表 */
    getList() {
      this.loading = true;
      listVocabulary(this.queryParams).then(response => {
        this.vocabularyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        word: null,
        projectId: null,
        isDelete: null,
        createTime: null,
        updateTime: null,
        phonetics: null,
        voice: null,
        groupNo: null,
        source: null,
        vocabularyBookId: null,
        status: null,
        sentenceDictionary: null,
        wordClassDictionary: null,
        synonymDictionary: null,
        meaningDictionary: null,
        translationDictionary: null,
        paradigmId: null,
        wordCategory: null,
        wordCategoryTest: null,
        sentence: null,
        storyGroupNo: null,
        storyGroupExampleSentence: null,
        storyMain: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加正式词库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVocabulary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改正式词库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVocabulary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVocabulary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除正式词库编号为"' + ids + '"的数据项？').then(function() {
        return delVocabulary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/word-memory/vocabulary/export', {
        ...this.queryParams
      }, `vocabulary_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
